package com.ruoyi.basic.service.impl;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.basic.domain.BasicFamily;
import com.ruoyi.basic.domain.BasicUser;
import com.ruoyi.basic.domain.BasicWork;
import com.ruoyi.basic.mapper.BasicUserMapper;
import com.ruoyi.basic.mapper.BasicWorkMapper;
import com.ruoyi.basic.service.IBasicWorkService;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.common.core.text.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 工作经历Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-08-22
 */
@Service
public class BasicWorkServiceImpl extends ServiceImpl<BasicWorkMapper, BasicWork> implements IBasicWorkService
{
    @Autowired
    private BasicUserMapper userMapper;

    /**
     * 查询工作经历
     * 
     * @param workId 工作经历主键
     * @return 工作经历
     */
    @Override
    public BasicWork selectBasicWorkByWorkId(Long workId)
    {
        return baseMapper.selectBasicWorkById(workId);
    }

    /**
     * 查询工作经历列表
     * 
     * @param basicWork 工作经历
     * @return 工作经历
     */
    @Override
    public List<BasicWork> selectBasicWorkList(BasicWork basicWork)
    {
        return baseMapper.selectBasicWorkList(basicWork);
    }

    /**
     * 新增工作经历
     * 
     * @param basicWork 工作经历
     * @return 结果
     */
    @Override
    public int insertBasicWork(BasicWork basicWork)
    {
        basicWork.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(basicWork);
    }

    /**
     * 修改工作经历
     * 
     * @param basicWork 工作经历
     * @return 结果
     */
    @Override
    public int updateBasicWork(BasicWork basicWork)
    {
        basicWork.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(basicWork);
    }

    /**
     * 批量删除工作经历
     * 
     * @param workIds 需要删除的工作经历主键
     * @return 结果
     */
    @Override
    public int deleteBasicWorkByWorkIds(String workIds)
    {
        return baseMapper.deleteBatchIds(Arrays.asList(Convert.toLongArray(workIds)));
    }

    /**
     * 删除工作经历信息
     * 
     * @param workId 工作经历主键
     * @return 结果
     */
    @Override
    public int deleteBasicWorkByWorkId(Long workId)
    {
        return baseMapper.deleteById(workId);
    }

    /**
     * 导入用户工作经历
     *
     * @param workList 用户工作经历数据列表
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importWork(List<BasicWork> workList, Boolean isUpdateSupport, String operName)
    {
        if (StringUtils.isNull(workList) || workList.size() == 0)
        {
            throw new ServiceException("导入用户工作经历数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (BasicWork work : workList)
        {
            int row = 1;
            try
            {
                if(StringUtils.isEmpty(work.getWorkDept())){
                    continue;
                }
                //获取用户信息
                BasicUser user = userMapper.selectOne(new LambdaQueryWrapper<BasicUser>()
                        .eq(BasicUser::getIdCard, work.getIdCard()).last("limit 1"));
                if(StringUtils.isNotNull(user)) {
                    work.setUserId(user.getId());
                }else{
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、第 " + row + "行导入失败，员工 " + work.getUserName() + " 不存在");
                    continue;
                }
                BasicWork info = baseMapper.selectOne(new LambdaQueryWrapper<BasicWork>()
                        .eq(BasicWork::getWorkDept, work.getWorkDept())
                        .eq(BasicWork::getWorkPost, work.getWorkPost())
                        .eq(BasicWork::getUserId, work.getUserId()).last("limit 1"));
                if (StringUtils.isNull(info))
                {
                    work.setCreateBy(operName);
                    this.insertBasicWork(work);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工作经历 " + work.getWorkDept() + " 导入成功");
                }
                else if (isUpdateSupport)
                {
                    work.setWorkId(info.getWorkId());
                    work.setUpdateBy(operName);
                    this.updateBasicWork(work);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、工作经历 " + work.getWorkDept() + " 更新成功");
                }
                else
                {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、工作经历 " + work.getWorkDept() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、员工 " + work.getUserName() + " 工作经历导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
            finally {
                row++;
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }
}
